package gui.visu.components;

import gui.IHM;
import gui.utils.RadioButtonIcon;
import gui.visu.listeners.ListenerAddDisplayDataInSpecificPositionButton;
import gui.visu.listeners.ListenerChooseDisplayDataColor;
import gui.visu.listeners.ListenerChooseMouseColor;
import gui.visu.listeners.ListenerChooseSelectionColor;
import gui.visu.listeners.ListenerAddDisplayDataCompareButton;
import gui.visu.listeners.ListenerModifyDisplayDataPositionDownButton;
import gui.visu.listeners.ListenerModifyDisplayDataPositionUpButton;
import gui.visu.listeners.ListenerNbOfCutsRadio;
import gui.visu.listeners.ListenerRemoveSelectedDisplayDataButton;
import gui.visu.listeners.ListenerSwitchSelectedDisplayDataButton;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;

import tuio.components.ButtonTouch;
import tuio.components.RadioButtonTouch;

import data.Constants;
import data.visu.utils.ImageLoader;


/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public class DisplayDataOptionPanelUp extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 141018542457014347L;
	private IHM ihm;
	
	private Dimension dimButtons;

	public DisplayDataOptionPanelUp(IHM parent){
		this.setIhm(parent);
		int side = parent.getVisuPane().getBounds().height/20;
		dimButtons = new Dimension(side,side);
		buildDisplayDataOptionPanelUp();
	}
	
	private void buildDisplayDataOptionPanelUp(){
		GridBagConstraints c = new GridBagConstraints();
		c.gridx=0;
		c.gridy=0;
		this.setLayout(new GridBagLayout());
		
		c.fill=GridBagConstraints.BOTH;
		c.weightx=1;
		c.weighty=1;
		//c.insets = Constants.INSETS_OPTION_BUTTON;
		
		c.gridwidth=3;
		//actions sur les courbes - switch, add, delete, modify position
		this.add(buildDisplayDataInteractionPanel(),c);
		
		c.weighty=0;
		c.gridx=0;
		c.gridy=c.gridy+1;
		this.add(new JSeparator(JSeparator.HORIZONTAL),c);
		
		c.weighty=1;
		c.gridwidth=1;
		c.gridx=0;
		c.gridy=c.gridy+1;
		this.add(buildModifyNbOfCutsPane(),c);

		c.gridx=c.gridx+1;
		c.weightx=0;
		this.add(new JSeparator(JSeparator.VERTICAL),c);
		
		c.weightx=1;
		c.gridx=c.gridx+1;
		this.add(buildColorPane(),c);
	}
	

	
	private JPanel buildDisplayDataInteractionPanel() {
		JPanel displayDataInteractionPanel = new JPanel(new GridBagLayout());
		displayDataInteractionPanel.setName("DisplayDataInteraction");
		GridBagConstraints c = new GridBagConstraints();
		c.weightx=1;
		c.insets = Constants.INSETS_OPTION_BUTTON;
		c.fill = GridBagConstraints.NONE;
		c.gridx=0;
		c.gridy=0;
		
		ButtonTouch addDisplayData = new ButtonTouch(ImageLoader.createImageIcon("curve_add.png"));
		addDisplayData.addActionListener(new ListenerAddDisplayDataInSpecificPositionButton(ihm.getDataControl()));
		addDisplayData.setPreferredSize(dimButtons);
		addDisplayData.setToolTipText("Add data");
		displayDataInteractionPanel.add(addDisplayData, c);
		
		c.gridx = c.gridx + 1;
		
		ButtonTouch removeSelectedDisplayData = new ButtonTouch(ImageLoader.createImageIcon("curve_remove.png"));
		removeSelectedDisplayData.addActionListener(new ListenerRemoveSelectedDisplayDataButton(ihm.getDataControl()));
		removeSelectedDisplayData.setPreferredSize(dimButtons);
		removeSelectedDisplayData.setToolTipText("Remove data");
		displayDataInteractionPanel.add(removeSelectedDisplayData, c);
		
		c.gridx=c.gridx+1;
		
		ButtonTouch moveDownSelectedDisplayData = new ButtonTouch(ImageLoader.createImageIcon("curve_down.png"));
		moveDownSelectedDisplayData.addActionListener(new ListenerModifyDisplayDataPositionDownButton(ihm.getDataControl()));
		moveDownSelectedDisplayData.setPreferredSize(dimButtons);
		moveDownSelectedDisplayData.setToolTipText("Move down data");
		displayDataInteractionPanel.add(moveDownSelectedDisplayData, c);
		
		c.gridx=c.gridx+1;
		
		ButtonTouch moveUpSelectedDisplayData = new ButtonTouch(ImageLoader.createImageIcon("curve_up.png"));
		moveUpSelectedDisplayData.addActionListener(new ListenerModifyDisplayDataPositionUpButton(ihm.getDataControl()));
		moveUpSelectedDisplayData.setPreferredSize(dimButtons);
		moveUpSelectedDisplayData.setToolTipText("Move up data");
		displayDataInteractionPanel.add(moveUpSelectedDisplayData, c);
		
		c.gridx=c.gridx+1;
		
		ButtonTouch switchSelectedDisplayData = new ButtonTouch(ImageLoader.createImageIcon("curves_switch.png"));
		switchSelectedDisplayData.addActionListener(new ListenerSwitchSelectedDisplayDataButton(ihm.getDataControl()));
		switchSelectedDisplayData.setPreferredSize(dimButtons);
		switchSelectedDisplayData.setToolTipText("Switch selected datas");
		displayDataInteractionPanel.add(switchSelectedDisplayData, c);
		
		c.gridx=c.gridx+1;
		
		ButtonTouch buttonCompare = new ButtonTouch(ImageLoader.createImageIcon("curve_compare.png"));
		buttonCompare.addActionListener(new ListenerAddDisplayDataCompareButton(ihm.getDataControl()));
		buttonCompare.setPreferredSize(dimButtons);
		buttonCompare.setToolTipText("Compare datas");
		displayDataInteractionPanel.add(buttonCompare, c);
		
		return displayDataInteractionPanel;
	}


	private JPanel buildModifyNbOfCutsPane(){
		JPanel modifyNbOfCuts = new JPanel(new GridBagLayout());
		modifyNbOfCuts.setName("modifyNbOfCuts");
		GridBagConstraints c = new GridBagConstraints();
		c.gridy=0;
		c.gridx=0;
		c.fill=GridBagConstraints.NONE;
		//c.insets = Constants.INSETS_OPTION_BUTTON;
		JLabel nbOfCutsLabel = new JLabel("Number of cuts:");
		nbOfCutsLabel.setFont(Constants.OPTION_PANE_FONT);
		modifyNbOfCuts.add(nbOfCutsLabel,c);
//		c.gridx=1;
//		ComboBoxTouch nbOfCutsComboBox = new ComboBoxTouch();
//		for(int i=0;i<Constants.POSSIBLE_NB_OF_CUT+1;i++){
//			nbOfCutsComboBox.addItem(""+i);
//		}
//		nbOfCutsComboBox.setSelectedIndex(Constants.DISPLAY_DATA_DEFAULT_NB_CUT_FOR_ALL_CURVES);
//		nbOfCutsComboBox.setToolTipText("Modify curves number of cuts");
//		nbOfCutsComboBox.addActionListener(new ListenerNbOfCutsComboBox(ihm.getDataControl()));
//
//		modifyNbOfCuts.add(nbOfCutsComboBox,c);

		Dimension dimRadio = new Dimension(45,45);
		
		c.gridx=c.gridx+1;
		RadioButtonTouch cut0 = new RadioButtonTouch(new RadioButtonIcon("cut0", true));
		cut0.addActionListener(new ListenerNbOfCutsRadio(ihm.getDataControl(), 0));
		cut0.setPreferredSize(dimRadio);
		cut0.setSelected(true);
		modifyNbOfCuts.add(cut0,c);
		
		c.gridx=c.gridx+1;
		RadioButtonTouch cut1 = new RadioButtonTouch(new RadioButtonIcon("cut1", true));
		cut1.addActionListener(new ListenerNbOfCutsRadio(ihm.getDataControl(), 1));
		cut1.setPreferredSize(dimRadio);
		cut1.setSelected(false);
		modifyNbOfCuts.add(cut1,c);
		
		c.gridx=c.gridx+1;
		RadioButtonTouch cut2 = new RadioButtonTouch(new RadioButtonIcon("cut2", true));
		cut2.addActionListener(new ListenerNbOfCutsRadio(ihm.getDataControl(), 2));
		cut2.setPreferredSize(dimRadio);
		cut2.setSelected(false);
		modifyNbOfCuts.add(cut2,c);
		
		c.gridx=c.gridx+1;
		RadioButtonTouch cut3 = new RadioButtonTouch(new RadioButtonIcon("cut3", true));
		cut3.addActionListener(new ListenerNbOfCutsRadio(ihm.getDataControl(), 3));
		cut3.setPreferredSize(dimRadio);
		cut3.setSelected(false);
		modifyNbOfCuts.add(cut3,c);
		
		ButtonGroup group = new ButtonGroup();
		group.add(cut0);
		group.add(cut1);
		group.add(cut2);
		group.add(cut3);
		
		return modifyNbOfCuts;
	}
	
	private JPanel buildDisplayDataColorPanel(){
		JPanel pane = new JPanel();

		final ButtonTouch changeDisplayDataColor = new ButtonTouch(ImageLoader.createImageIcon("curve_color_red.png"));
		changeDisplayDataColor.setPreferredSize(dimButtons);
		changeDisplayDataColor.addActionListener(new ListenerChooseDisplayDataColor(ihm.getDataControl()));
		pane.add(changeDisplayDataColor);

		return pane;
	}
	
	private JPanel buildSelectionColorPanel(){
		JPanel pane = new JPanel();

		final ButtonTouch changeSelectionColor = new ButtonTouch(ImageLoader.createImageIcon("selection_color_red.png"));
		changeSelectionColor.setPreferredSize(dimButtons);
		changeSelectionColor.addActionListener(new ListenerChooseSelectionColor(ihm.getDataControl(),this));
		pane.add(changeSelectionColor);

		return pane;
	}
	
	private JPanel buildMouseColorPanel(){
		JPanel pane = new JPanel();

		final ButtonTouch changeMouseColor = new ButtonTouch(ImageLoader.createImageIcon("mouse_color_red.png"));
		changeMouseColor.setPreferredSize(dimButtons);
		changeMouseColor.addActionListener(new ListenerChooseMouseColor(ihm.getDataControl(),this));
		pane.add(changeMouseColor);

		return pane;
	}

	private JPanel buildColorPane(){
		JPanel colorPane = new JPanel(new GridBagLayout());
		colorPane.setName("colorPane");
		GridBagConstraints c = new GridBagConstraints();
		c.insets = Constants.INSETS_OPTION_BUTTON;

		c.gridx=0;
		colorPane.add(buildDisplayDataColorPanel(),c);

		c.gridx=c.gridx+1;
		colorPane.add(buildMouseColorPanel(),c);

		c.gridx=c.gridx+1;
		colorPane.add(buildSelectionColorPanel(),c);

		return colorPane;
	}

	public void setIhm(IHM ihm) {
		this.ihm = ihm;
	}

	public IHM getIhm() {
		return ihm;
	}
}
